package com.hotel.search.lucene;

import com.hotel.comm.bean.Constants;
import com.hotel.comm.lucene.condition.ICondition;
import com.hotel.comm.lucene.condition.SearchSort;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;

/**
 * lucene封装搜索条件类
 *
 * @author XuSemon
 * @date 2015-4-24
 */
public class LuceneSearchCondition {
    /**
     * 分页当前页
     */
    private int currentPage;
    /**
     * 每页显示行数
     */
    private int pageSize;
    /**
     * 查询Query
     */
    private Query query = null;
    /**
     * 排序Sort
     */
    private Sort sort = new Sort();

    /**
     * 默认构造函数
     */
    public LuceneSearchCondition() {
    }

    /**
     * 构造函数
     *
     * @param searchSorts
     * @param condition
     */
    public LuceneSearchCondition(SearchSort[] searchSorts, ICondition condition) {
        this(1, searchSorts, condition);
    }

    /**
     * 构造函数
     *
     * @param currentPage
     * @param searchSorts
     * @param condition
     */
    public LuceneSearchCondition(int currentPage, SearchSort[] searchSorts, ICondition condition) {
        this(currentPage, Constants.DEFAULT_PAGE_SIZE, searchSorts, condition);
    }

    /**
     * 构造函数
     *
     * @param currentPage
     * @param pageSize
     * @param searchSorts
     * @param condition
     */
    public LuceneSearchCondition(int currentPage, int pageSize, SearchSort[] searchSorts, ICondition condition) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        query = condition.getQuery();
        if (searchSorts != null && searchSorts.length > 0) {
            SortField[] sortFields = new SortField[searchSorts.length];
            for (int i = 0; i < searchSorts.length; i++) {
                SearchSort searchSort = searchSorts[i];
                sortFields[i] = new SortField(searchSort.getSortName(), searchSort.getType(), searchSort.getReverse().isDesc());
            }
            sort.setSort(sortFields);
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Query getQuery() {
        return query;
    }

    public void setQuery(BooleanQuery query) {
        this.query = query;
    }

    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }
}
